<template>
  <div>
    <van-nav-bar class="top-nav-bar" title="登录页面" />
    <!-- 表单 start -->
    <van-form class="form" ref="form" @submit="onSubmit" :show-error="false">
      <div class="form-item">
        <van-field
          v-model="userInfo.username"
          name="用户名"
          label="用户名："
          placeholder="请输入用户名"
          @focus="usernameMsg = true"
          @blur="usernameMsg = false"
          :rules="[
            {
              required: true,
              message: '用户名6-16个字符',
              pattern: /^.{6,16}$/,
            },
          ]"
        />
        <p class="msg" v-show="usernameMsg">
          6~18个字符，可使用字母、数字、下划线，需要以字母开头
        </p>
      </div>
      <div class="form-item">
        <van-field
          v-model="userInfo.password"
          type="password"
          name="密码"
          label="密码："
          placeholder="请输入密码"
          @focus="passwordMsg = true"
          @blur="passwordMsg = false"
          :rules="[
            {
              required: true,
              message: '密码6-16个字符',
              pattern: /^.{6,16}$/,
            },
          ]"
        />
        <p class="msg" v-show="passwordMsg">
          6~18个字符，可使用字母、数字、下划线，需要以字母开头
        </p>
      </div>
      <van-button
        tag="a"
        color="#FB7299"
        plain
        @click="$router.push('/register')"
        >去注册</van-button
      >
      <van-button
        color="#FB7299"
        :disabled="isBtnDisabled"
        type="info"
        native-type="submit"
        >登录</van-button
      >
    </van-form>
    <!-- 表单 end -->
  </div>
</template>

<script>
import { getLoginAPI } from "@/network/api";

import Vue from "vue";
import { Form, Button, Field, NavBar, Toast } from "vant";
Vue.use(Form);
Vue.use(Button);
Vue.use(Field);
Vue.use(NavBar);

export default {
  name: "Login",
  data() {
    return {
      userInfo: {
        username: "",
        password: "",
      },
      usernameMsg: false,
      passwordMsg: false,
      isBtnDisabled: true,
    };
  },
  methods: {
    // 点击登录发送axios
    onSubmit(values) {
      getLoginAPI(this.userInfo)
        .then((res) => {
          console.log(res.data);
          // 登录成功
          if (res.data.code == 200) {
            Toast.success(res.data.msg);
            // 登录成功保存id和token到本地存储,然后1秒后跳转个人中心
            localStorage.setItem("id", res.data.id);
            localStorage.setItem("token", res.data.token);
            setTimeout(() => {
              this.$router.push("/Personal");
            }, 500);
          }
          // 用户名不存在 或者 密码错误
          else if (res.data.code == 301 || res.data.code == 302) {
            Toast.fail(res.data.msg);
          }
        })
        .catch((err) => {
          console.log(err);
          Toast.fail(res.data);
        });
    },
  },
  watch: {
    // 监听userInfo数据变化
    userInfo: {
      handler(newValue, oldValue) {
        if (newValue.username != "" && newValue.password != "") {
          this.isBtnDisabled = false;
        } else {
          this.isBtnDisabled = true;
        }
      },
      deep: true,
    },
  },
};
</script>

<style scoped  lang="less" >
.top-nav-bar {
  margin-bottom: 10px;
}
.van-form {
  text-align: center;
}
.form-item {
  position: relative;
  padding-bottom: 20px;
  .msg {
    position: absolute;
    font-size: 12px;
    color: #9e9e9e;
    text-indent: 10px;
  }
}
.van-button {
  width: 120px;
  border-radius: 5px;
  margin: 0 10px;
}
</style>